"""配置接口,需要判断配置是否发生改变,变则配置版本变化,反之不变"""
from rest_framework.response import Response
from rest_framework.views import APIView
from django_redis import get_redis_connection
# TODO 每个视图函数缺少验证token值
from bms_core.create_token import CheckToken


class GetConfigData(APIView):
    def post(self,request):
        version = int(request.data.get('config_version'))
        token = request.META.get('HTTP_TOKEN')
        context = CheckToken().check_token(token)
        if not context:
            return Response({"result":'Failed'})
        # TODO 缺少验证token值
        # 返回配置信息
        data = {

        }
        return Response({"data":data})


class DingAlarm(APIView):
    def post(self,request):
        token = request.META.get("HTTP_TOKEN")
        subscribe = request.data.get('subscribe')
        context = CheckToken().check_token(token)
        if not context:
            return Response({"data":{"result":'Failed'}})
        r = get_redis_connection('default')
        username = context['username']
        r.hset('dingalarm',username,subscribe)
        data = {
            'result':'True'
        }
        return Response({'data':data})


class DingPM(APIView):
    def post(self,request):
        token = request.META.get('HTTP_TOKEN')
        guid_list = request.data.get('guid')
        context = CheckToken().check_token(token)
        if not context:
            return Response({"data":{"result":'Failed'}})
        username = context['username']
        r = get_redis_connection('default')
        r.hset('guid_%s'%username,'guid',guid_list)
        return Response({'data':{'result':'success'}})


class PMModel(APIView):
    def post(self,request):
        token = request.META.get('HTTP_TOKEN')
        mode = request.data.get('mode')
        period = request.data.get('period')
        context = CheckToken().check_token(token)
        if not context:
            return Response({"data":{"result":'Failed'}})
        username = context['username']
        r = get_redis_connection('default')
        r.hset('mode_%s'%username,mode,period)
        return Response({'data':{"result":'success'}})